package com.comstartest.iotest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.comstar.cstgpio.GPIOJNI;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    int mun = 5;
    private Button bt_start;
    private Button bt_cancel;
    private TextView tv_info;
    private static final String TAG = "Watchdog";
    int code1;
    int code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        code1 = GPIOJNI.openGpioDev();
        code = GPIOJNI.requestGpio(mun);
        bt_start = findViewById(R.id.bt_start);
       // bt_stop = findViewById(R.id.bt_stop);
        bt_cancel = findViewById(R.id.bt_cancel);
        tv_info = findViewById(R.id.tv_info);

        bt_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (code1 != 0) {
                    GPIOJNI.openGpioDev();
                }
                if (code != 0) {
                    GPIOJNI.requestGpio(mun);
                }
                GPIOJNI.setGpioDirection(mun, 1);
                openCount = 12;
                mHandler.sendEmptyMessage(1);
            }
        });
      /*  bt_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeCount = 10;
                mHandler.sendEmptyMessage(3);
            }
        });*/

        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHandler.sendEmptyMessage(6);
            }
        });
        if (code1 != 0 || code != 0) {
            GPIOJNI.releaseGpio(mun);
            return;
        }

    }

    int openCount = 0;
    int closeCount = 0;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {

                case 1://开始
                    Log.d(TAG, "watchdog send 1");
                    tv_info.setText("开启中");
                    mHandler.removeMessages(2);
                    if (openCount < 1) {// 倒计时完毕则发送2
                        mHandler.sendEmptyMessageDelayed(2, 18000);
                        tv_info.setText("开启动作完成");
                        return;
                    }
                    mHandler.sendEmptyMessage(4);
                    mHandler.sendEmptyMessageDelayed(1, 100);
                    openCount--;
                    break;
                case 2://保持
                    Log.d(TAG, "watchdog send 2");
                    tv_info.setText("看门狗工作中");
                    mHandler.sendEmptyMessageDelayed(2, 15000);
                    mHandler.sendEmptyMessage(4);
                    break;
                case 3://关闭喂狗
                    tv_info.setText("开始关闭喂狗");
                    Log.d(TAG, "watchdog send 3");
                    mHandler.removeMessages(1);
                    mHandler.removeMessages(2);
                    mHandler.removeMessages(4);
                    mHandler.removeMessages(5);
                    if (closeCount < 1) {
                        tv_info.setText("关闭喂狗成功");
                        GPIOJNI.releaseGpio(mun);
                        return;
                    }
                    closeCount--;
                    mHandler.sendEmptyMessage(4);
                    mHandler.sendEmptyMessageDelayed(3, 3000);
                    break;
                // 拉高，50ms后拉低
                case 4://拉高
                    Log.d(TAG, "watchdog send high");
                    GPIOJNI.setGpioState(mun, 1);
                    mHandler.sendEmptyMessageDelayed(5, 50);
                    break;
                case 5://拉低
                    Log.d(TAG, "watchdog send low");
                    GPIOJNI.setGpioState(mun, 0);
                    break;
                case 6:
                    tv_info.setText("停止喂狗信号");
                    Log.d(TAG, "watchdog send 6");
                    mHandler.removeMessages(2);
                    mHandler.removeMessages(4);
                    mHandler.removeMessages(5);
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeMessages(1);
            mHandler.removeMessages(2);
            mHandler.removeMessages(3);
            mHandler.removeMessages(4);
            mHandler.removeMessages(5);
            mHandler.removeMessages(6);
            mHandler = null;
        }
        super.onDestroy();
    }
}
